/*
 * MouseFilterEvents.cpp
 *
 *  Created on: Apr 29, 2010
 *      Author: pannay
 */
#include"MouseFilterEvents.h"

QMouseFilterEvents::QMouseFilterEvents(QObject *parent,
		Qt::MouseButton gestureButton) :
	QObject(parent)
	{
	m_parent = static_cast<QTAvkonView *> (parent);
	m_parent->setVertical(false);
	mouseMoveCount = 0;
	verMode=false;
	}
QMouseFilterEvents::~QMouseFilterEvents()
	{
	
	}
bool QMouseFilterEvents::eventFilter(QObject *obj, QEvent *event)
	{
	switch (event->type())
		{
		case QEvent::MouseButtonPress:
			if (mouseButtonPressEvent(obj, dynamic_cast<QMouseEvent*> (event)))
				return true;

			break;

		case QEvent::MouseButtonRelease:
			if (mouseButtonReleaseEvent(obj, dynamic_cast<QMouseEvent*> (event)))
				return true;

			break;

		case QEvent::MouseMove:
			if (mouseMoveEvent(obj, dynamic_cast<QMouseEvent*> (event)))
				return true;

			break;

		default:
			break;
		}

	return QObject::eventFilter(obj, event);
	}

bool QMouseFilterEvents::mouseButtonPressEvent(QObject *obj, QMouseEvent *event)
	{
	xstart = (static_cast<QMouseEvent*> (event))->pos().x();
	ystart = (static_cast<QMouseEvent*> (event))->pos().y();
	return true;
	}

bool QMouseFilterEvents::mouseButtonReleaseEvent(QObject *obj,
		QMouseEvent *event)
	{
	xEnd = (static_cast<QMouseEvent*> (event))->pos().x();
	yEnd = (static_cast<QMouseEvent*> (event))->pos().y();
	if (mouseMoveCount > minMouseMove)
		{
		GestureDirection();
		}
	mouseMoveCount = 0;
	return true;
	}
bool QMouseFilterEvents::mouseMoveEvent(QObject *obj, QMouseEvent *event)
	{
	mouseMoveCount++;
	return true;
	}
void QMouseFilterEvents::NslideInPrev()
	{
	m_parent->SlidePrv();

	}
void QMouseFilterEvents::NslideInNext()
	{
	m_parent->SlideNext();
	}
//sets vertical mode to true.

void QMouseFilterEvents::setVertical(bool v)
	{
	m_parent->setVertical(v);
	}
void QMouseFilterEvents::GestureDirection()
	{
	int Xdist = 0;
	int Ydist = 0;

	Xdist = xEnd - xstart;
	Ydist = yEnd - ystart;
	
	verMode = m_parent->getVertical();

	t_direction direction;
	if (abs(Xdist) > abs(Ydist))
		{
		if (Xdist > 0)
			direction = LEFT2RIGHT;
		else
			direction = RIGHT2LEFT;
		}
	else if (!(abs(Xdist) > abs(Ydist)))
		{
		if (Ydist > 0)
			direction = TOP2BOTTOM;
		else
			direction = BOTTOM2TOP;
		}
	switch (direction)
		{
		case LEFT2RIGHT:
			{
			setVertical(false);
			NslideInPrev();
			m_parent->setVertical(verMode);
			break;
			}
		case RIGHT2LEFT:
			{
			setVertical(false);
			NslideInNext();
			m_parent->setVertical(verMode);
			break;
			}
		case TOP2BOTTOM:
			{
			setVertical(true);
			NslideInNext();
			m_parent->setVertical(verMode);
			break;
			}
		case BOTTOM2TOP:
			{
			setVertical(true);
			NslideInPrev();
			m_parent->setVertical(verMode);
			break;
			}

		}
	}

